package subumbo.demo.controller {
	import org.robotlegs.mvcs.Command;
	
	import subumbo.demo.controller.signal.PreviewSignal;
	import subumbo.demo.model.WishModel;
	import subumbo.demo.service.LoadWishService;
	import subumbo.demo.view.ViewPool;
	import subumbo.demo.view.logo.Logo;
	import subumbo.demo.view.selector.Selector;

	public class StartupCommand extends Command {
		
		[Inject] public var viewPool:ViewPool;
		[Inject] public var loadWishService:LoadWishService;
		[Inject] public var wishModel:WishModel;
		[Inject] public var previewSignal:PreviewSignal;
		
		public override function execute():void {
			
			var id:String = contextView.stage.loaderInfo.parameters.id;
			if(id) {
				commandMap.detain(this);
				trace(id);
				loadWishService.id = id;
				loadWishService.result.add(onWishResult);
				loadWishService.send();
			}else {
				viewPool.selector = new Selector();
				contextView.addChild(viewPool.selector);
			}
			contextView.addChild(new Logo());
		}
		
		private function onWishResult(wishes:String):void {
			var array:Array = wishes.split(",");
			var i:int; var l:int = array.length;
			for(; i < l; ++i) {
				wishModel["wish"+ (i+1)] = array[i];
			}
			previewSignal.dispatch();
			commandMap.release(this);
		}
	}
}